// public path inject
import Vue from 'vue'
import App from './App.vue'
import store from '@/store'
import { router } from '@/router/index'
import cui from '@cci/cui'
import CityUI from '@cci/city-ui'

import '@cci/city-ui/lib/theme-chalk/index.css'
/* ant的ui样式 */
import '@cci/theme-ant-design/src/index.scss'
/* cp组件的样式 */
import '@cci/cp-theme-default/src/index.scss'
import './styles/cui.scss'
import './styles/index.scss'
import './styles/reset.scss'
import './styles/flex.scss'
import './styles/unit.scss'
import PrototypeJs from '@/api/js/index.js'
// 包好UI框架默认行为的改动如【CascaderNode】
import '@/core/use.js'

import { startMicroApp, sendMicroData, EventType, isMicroApp, useWindow } from '@/micro-app'

Vue.use(PrototypeJs)
Vue.use(cui)
Vue.use(CityUI)
Vue.config.productionTip = false
useWindow().eventBus = new Vue()

startMicroApp()

let app = null

document.title = '预警研判系统'

/**
 * 应用每次进入都会调用 mount 方法，通常我们在这里触发应用的渲染方法
 */
// 👇 将渲染操作放入 mount 函数 -- 必填
function mount () {
  app = new Vue({
    router,
    store,
    watch: {
      // 向主应用发送路由数据（因为主应用设置的通配符路由无法检测到路由变化）
      $route: {
        // immediate: true,
        handler (route) {
          sendMicroData(EventType.ROUTER, { route, router })
        }
      }
    },
    render: h => h(App)
  }).$mount(`#${process.env.VUE_APP_NAME}_app`)
}

// 👇 将卸载操作放入 unmount 函数 -- 必填
function unmount () {
  // 清空当前子应用所有绑定函数
  window.microApp.clearDataListener()
  app.$destroy()
  app.$el.innerHTML = ''
  app = null
}

// 微前端环境下，注册mount和unmount方法
if (isMicroApp()) {
  window[`micro-app-${window.__MICRO_APP_NAME__}`] = { mount, unmount }
} else {
  // 非微前端环境直接渲染
  mount()
}
if (process.env.NODE_ENV === 'development') {
  // vue-devtools 显示子应用 加入此代码 qiankun
  const subDiv = document.createElement('div')
  subDiv.__vue__ = app
  document.body.appendChild(subDiv)
}

// 更新角标信息
export function updateStatis (data) {
  sendMicroData(EventType.STATIS, { eventStatis: data })
}
